package com.atguigu.delivery.dao.impl;

import com.atguigu.delivery.dao.BaseDao;
import com.atguigu.delivery.dao.api.DeliveryDao;
import com.atguigu.delivery.entity.Delivery;

import java.util.List;

public class DeliveryDaoImpl extends BaseDao implements DeliveryDao {
    @Override
    public List<Delivery> selectDeliveryListByUserId(Integer userId) {

        // 1、编写 SQL 语句
        String sql = """
                select 
                    id, 
                    delivery_name deliveryName, 
                    company_id companyId,
                    phone, 
                    address, 
                    send_time sendTime, 
                    state 
                from sys_delivery
                where user_id=?
                """;

        // 2、调用父类方法执行查询（此时查询到的数据仅仅只有 Delivery 本身，没有级联的 Company）
        List<Delivery> deliveryList = baseQuery(Delivery.class, sql, userId);

        return deliveryList;
    }

    @Override
    public void deleteDeliveryById(String id) {
        String sql = "delete from sys_delivery where id=?";
        baseUpdate(sql, id);
    }

    @Override
    public void insertDelivery(Delivery delivery) {
        String sql = """
                insert into sys_delivery(delivery_name, user_id, company_id, phone, address, send_time, state)
                values (?,?,?,?,?,?,?)
                """;
        baseUpdate(sql,
                delivery.getDeliveryName(),
                delivery.getUserId(),
                delivery.getCompanyId(),
                delivery.getPhone(),
                delivery.getAddress(),
                delivery.getSendTime(),
                delivery.getState());
    }

    @Override
    public Delivery selectDeliveryById(String id) {

        // 1、编写 SQL 语句
        String sql = """
                select 
                    id, 
                    delivery_name deliveryName, 
                    company_id companyId,
                    phone, 
                    address, 
                    send_time sendTime, 
                    state 
                from sys_delivery
                where id=?
                """;

        // 2、执行查询
        List<Delivery> deliveryList = baseQuery(Delivery.class, sql, id);

        return deliveryList.get(0);
    }

    @Override
    public void updateDelivery(Delivery delivery) {
        String sql = """
                update sys_delivery
                set company_id=?,
                    address=?,
                    delivery_name=?,
                    send_time=?,
                    state=?,
                    phone=?
                where id = ?
                """;
        baseUpdate(sql, delivery.getCompanyId(),
                delivery.getAddress(),
                delivery.getDeliveryName(),
                delivery.getSendTime(),
                delivery.getState(),
                delivery.getPhone(),
                delivery.getId());
    }
}
